package com.ivan.stu.notetool.adapter;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.ivan.stu.notetool.R;


/**
 * Created by IVAN on 2019/3/27.
 */

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.MyViewHolder>{

    private int[] imageList;

    private int mSelected = 1;

    private Context mContext;

    private onImageClickListener listener;

      public ImageAdapter(Context context, int[] imageList){
        this.imageList = imageList;
        mContext = context;
    }


    public interface onImageClickListener{

          void onImageClick(int position, int resourceId);
          void onChoosePicClick();
    }


    public  void setImageClickListener(onImageClickListener listener){

          this.listener  = listener;
    }


    public void resetPosition(){

        mSelected = 0;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

       View itemView = LayoutInflater.from(mContext).inflate(R.layout.notetool_pic_item,parent,false);
        return new MyViewHolder(itemView);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {

          //异步加载图片
        Object tag=((MyViewHolder) holder).imageView.getTag(R.id.imageloader_uri);

        if (tag!=null&&(int) tag!= position) {
            //如果tag不是Null,并且同时tag不等于当前的position。
            //说明当前的viewHolder是复用来的
            Glide.with(mContext).clear(((MyViewHolder) holder).imageView);
        }
        Integer url = imageList[position];

        Glide.with(mContext)
                .load(url)
                .into(((MyViewHolder) holder).imageView);

        //给ImageView设置唯一标记。

        ((MyViewHolder) holder).imageView.setTag(R.id.imageloader_uri, position);

        if (position == mSelected){

            if (position!=0){

                holder.imageView.setPadding(4,4,4,4);
                holder.imageView.setBackground(mContext.getResources().getDrawable(R.drawable.notetool_pic_bg_selected));
            }

        }else {

            if (position!=0){

                holder.imageView.setPadding(1,1,1,1);
                holder.imageView.setBackground(mContext.getResources().getDrawable(R.drawable.notetool_pic_bg_unselected));
            }


        }



        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (position == 0){  //打开相册

                    if (listener!=null){

                        listener.onChoosePicClick();

                    }

                }else {  //直接替换画板图片



                    if (listener!=null){

                        listener.onImageClick(position,imageList[position]);

                    }

                    mSelected = position;  //更新选中状态
                    notifyDataSetChanged();  //重绘
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return imageList.length;
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        private ImageView imageView;
        private LinearLayout linearLayout;

        public MyViewHolder(View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.iv_pic);
            linearLayout = itemView.findViewById(R.id.ll_pic);
        }
    }
}
